package com.share.tool.generator.http_page;

import com.alibaba.fastjson.JSONObject;
import com.share.common.utils.HttpUtil;
import com.share.common.utils.ThreadUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.Executor;
import java.util.function.Function;

@Slf4j
public class MainPage {
    private Executor executorService = ThreadUtil.getExecutor();

    //具体的业务
    public String requestElk(String dataType) {
        String url = "www.baidu.com";
        JSONObject param = new JSONObject();
        param.put("dataType", dataType);
        param.put("range", 20); //显示条数,注释该行不填写将使用默认10,最大20
        Function<Integer, String> f1 = (pageNo) -> {
            param.put("pageno", pageNo);//默认第一页
            try {
                return HttpUtil.post(url, "ss");
            } catch (Exception e) {
                log.error("调用异常", e);
                return null;
            }
        };

        //合并处理
        Function<List<String>, String> f2 = (results) -> {
            JSONObject obj = new JSONObject();
            for (String result : results) {
                //数据的合并处理
            }
            return obj.toString();
        };
        return HttpUtil.pageAll("$.pageTatal", f1, f2, executorService);
    }
}
